/*
 * Copyright (c) 2005, Haxe China community .
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY HAXE CHINA COMMUNITY "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL HAXE CHINA COMMUNITY BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 * 
 * Copyright (c) 1998 著作权由Haxe中国社区所有。著作权人保留一切权利。
 * 
 * 这份授权条款，在使用者符合以下三条件的情形下，授予使用者使用及再散播本
 * 软件包装原始码及二进位可执行形式的权利，无论此包装是否经改作皆然：
 * 
 * 对于本软件源代码的再散播，必须保留上述的版权宣告、此三条件表列，以
 * 及下述的免责声明。
 * 对于本套件二进位可执行形式的再散播，必须连带以文件以及／或者其他附
 * 于散播包装中的媒介方式，重制上述之版权宣告、此三条件表列，以及下述
 * 的免责声明。
 * 
 * 免责声明：本软件是由Haxe中国社区及本软件之贡献者以现状（"as is"）提供，
 * 本软件包装不负任何明示或默示之担保责任，包括但不限于就适售性以及特定目
 * 的的适用性为默示性担保。Haxe中国社区及本软件之贡献者，无论任何条件、
 * 无论成因或任何责任主义、无论此责任为因合约关系、无过失责任主义或因非违
 * 约之侵权（包括过失或其他原因等）而起，对于任何因使用本软件包装所产生的
 * 任何直接性、间接性、偶发性、特殊性、惩罚性或任何结果的损害（包括但不限
 * 于替代商品或劳务之购用、使用损失、资料损失、利益损失、业务中断等等），
 * 不负任何责任，即在该种使用已获事前告知可能会造成此类损害的情形下亦然。
 * 
 * 本许可证之中文译本中若有任何歧义，以英文版本为准。
 */
package com.haxecn.ui;

import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

/**
	The Button class.<br />
	Author Icebird<br />
	Detail: <br />
	Events：MouseEvent
**/
class Button extends Sprite {

	//click status defined on the flash IDE stage .
	var click : flash.display.MovieClip;
	//disable status defined on the flash IDE stage .
	var disable : flash.display.MovieClip;
	//hit area defined on the flash IDE stage .
	var hitMask : flash.display.MovieClip;
	//mouse over status defined on the flash IDE stage .
	var hover : flash.display.MovieClip;
	//normal status defined on the flash IDE stage .
	var normal : flash.display.MovieClip;
	//Mouse over status
	var mouseOver:Bool;
	//Mouse down status
	var mouseDown:Bool;

	/**
		set the button enabled or disabled
	**/
	public var enabled(getEnabled,setEnabled):Bool;

	function getEnabled():Bool
	{
		return this.mouseEnabled;
	}

	function setEnabled(value:Bool):Bool
	{
		this.mouseEnabled = value;
		this.mouseChildren = value;
		if(value)
		{
			click.visible = false;
			disable.visible = false;
			hover.visible = false;
			normal.visible = false;
			if(mouseDown)
			{
				click.visible = true;
			}else
			{
				if(mouseOver)
				{
					hover.visible = true;
				}
				else
				{
					normal.visible = true;
				}
			}
		}
		else
		{
			click.visible = false;
			disable.visible = true;
			hover.visible = false;
			normal.visible = false;
		}
		return value;
	}

	public function new() : Void
	{
		super();
		click.mouseEnabled = false;
		click.x = 0;
		click.y = 0;
		click.visible = false;
		
		disable.mouseEnabled = false;
		disable.x = 0;
		disable.y = 0;
		disable.visible = false;
		
		hover.mouseEnabled = false;
		hover.x = 0;
		hover.y = 0;
		hover.visible = false;
		
		hitMask.mouseEnabled = false;
		hitMask.x = 0;
		hitMask.y = 0;
		hitMask.visible = false;
		
		normal.mouseEnabled = false;
		normal.x = 0;
		normal.y = 0;
		
		this.hitArea = hitMask;
		hitMask.x = 0;
		hitMask.y = 0;
		hitMask.visible = false;
		
		this.useHandCursor = true;
		this.buttonMode = true;

		this.addEventListener(Event.ADDED_TO_STAGE, addToStageHandeler);
	}

	public function distroy():Void
	{
		if(this.stage == null)
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, addToStageHandeler);
		}
		else
		{
			//TODO throw a error
		}
	}

	//Add the mouse events listener when it added to stage
	function addToStageHandeler(e:Event):Void
	{
		this.addEventListener(MouseEvent.ROLL_OVER, rollOverHandeler);
		this.addEventListener(MouseEvent.ROLL_OUT, rollOutHandeler);
		this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandeler);
		this.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandeler);
		this.addEventListener(Event.REMOVED_FROM_STAGE, removeFromStageHandeler);
	}

	//Remove the mouse events listener when it remove from stage
	function removeFromStageHandeler(e:Event)
	{
		this.removeEventListener(MouseEvent.ROLL_OVER, rollOverHandeler);
		this.removeEventListener(MouseEvent.ROLL_OUT, rollOutHandeler);
		this.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandeler);
		this.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandeler);
		this.removeEventListener(Event.REMOVED_FROM_STAGE, removeFromStageHandeler);
	}

	function rollOverHandeler(e:MouseEvent):Void
	{
		mouseOver = true;
		if(!mouseDown){
			normal.visible = false;
			hover.visible = true;
		}
	}

	function rollOutHandeler(e:MouseEvent):Void
	{
		mouseOver = false;
			normal.visible = true;
			hover.visible = false;
		if(!mouseDown){
		}
	}

	function mouseDownHandeler(e:MouseEvent):Void
	{
		mouseDown = true;
		normal.visible = false;
		hover.visible = false;
		click.visible = true;
	}

	function mouseUpHandeler(e:MouseEvent):Void
	{
		mouseDown = false;
		click.visible = false;
		if(mouseOver)
		{
			hover.visible = true;
		}else
		{
			normal.visible = true;
		}
	}
}
